package com.smart.serializer;

import de.ruedigermoeller.serialization.FSTObjectInput;
import de.ruedigermoeller.serialization.FSTObjectOutput;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

//import org.nustaq.serialization.FSTConfiguration;

/**
 * FSTSerializer
 * -----------------------------------------------------------------------------------------------------------------------------------
 *
 * @author YRain
 */
public class FSTSerializer implements Serializer {

//    public static final Logger logger = LoggerFactory.getLogger(FSTSerializer.class);

    @Override
    public byte[] serialize(Object obj) {
        ByteArrayOutputStream out = null;
        FSTObjectOutput fout = null;
        try {
            out = new ByteArrayOutputStream();
            fout = new FSTObjectOutput(out);
            fout.writeObject(obj);
            byte[] arrayOfByte = out.toByteArray();

            return arrayOfByte;
        } catch (Exception e) {
            System.err.println("序列化失败:" + e);
            return null;
        } finally {
            if (fout != null)
                try {
                    fout.close();
                } catch (IOException e) {
                }
        }
    }

    @Override
    public Object deserialize(byte[] bytes) {
        if ((bytes == null) || (bytes.length == 0))
            return null;
        FSTObjectInput in = null;
        try {
            in = new FSTObjectInput(new ByteArrayInputStream(bytes));
            Object localObject1 = in.readObject();

            return localObject1;
        } catch (Exception e) {
            System.err.println("序列化失败:" + e);
            return null;
        } finally {
            if (in != null)
                try {
                    in.close();
                } catch (IOException e) {
                }
        }
    }

}
